package cn.itcast.controller;

import cn.itcast.pojo.OrderSetting;
import cn.itcast.service.OrderSettingService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;


import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 预约设置
 */
@RestController
@RequestMapping("/ordersetting")
@Slf4j
public class OrderSettingController {

    //远程调用服务
    @Reference
    private OrderSettingService orderSettingService;


    @GetMapping("/getOrderSettingsByMonth")
    public void getOrderSettingsByMonth(String date){
        log.info("预约设置的年份和月份date:{}",date);
        String[] s = StringUtils.split(date, "-");
        String year =  s[0];
        log.info("预约设置的年份year:{}",year);
        String month = s[1];
        log.info("预约设置的月份month:{}",month);
        List<OrderSetting> orderSettingsByMonth = this.orderSettingService.getOrderSettingsByMonth(year, month);
        log.info("查询成功的数据orderSettingsByMonth:{}",orderSettingsByMonth);

    }


    //单日预约设置
    @PutMapping("/updateOrderSettingNumber")
    public OrderSetting updateOrderSettingNumber(@RequestBody Map<String,String> params){
        OrderSetting orderSetting = null;
        try {
            String orderDate = params.get("orderDate");//获取日期
            String number = params.get("number");//从页面获取可以预约的人数
            log.info("预约设置的时间orderDate:{}",orderDate);
            log.info("预约设置的人数number:{}",number);

            //将参数交给service处理,将日期和预约人数保存到数据库中,并返回结果
            orderSetting = this.orderSettingService.dayOrderSetting(orderDate,number);
            if (null ==orderSetting){
                //没有数据
                return null;
            }
        } catch (Exception e) {
            log.info("e:{}",e);
        }
        return orderSetting;
    }
}
